'use strict';
import React, {Component} from 'react';
import {ScrollView, StyleSheet, Text, TextInput, ToastAndroid, View,} from 'react-native';
import TouchButton from "../widget/btn/HighlightButton";
import FileUtil from "../util/FileUtil";

const FILE_PATH = "RNLOG/log.txt";

/** react-native-filesystem */
export default class PageSdCardStorage extends Component {

    constructor(props) {
        super(props);
        this.state = {
            message: '',
        };
        this.textInfo = '';
    };

    render() {
        return (
            <ScrollView>
                <View style={styles.container}>
                    <TextInput
                        style={{height: 40, width: 200}}
                        placeholder="输入!"
                        onChangeText={(inputText) => {
                            this.textInfo = inputText;
                        }}
                    />

                    <View style={{height: 15}}/>
                    <TouchButton disabled={false} title={'查询绝对路径'} onPress={() => this._doGetPath()}/>

                    <View style={{height: 15}}/>
                    <TouchButton disabled={false} title={'测试文件是否存在'} onPress={() => this._doTestDoc()}/>

                    <View style={{height: 15}}/>
                    <TouchButton disabled={false} title={'测试文件夹是否存在'} onPress={() => this._doTestFolder()}/>

                    <View style={{height: 15}}/>
                    <TouchButton disabled={false} title={'新建文件'} onPress={() => this._doCreate()}/>

                    <View style={{height: 15}}/>
                    <TouchButton disabled={false} title={'删除文件'} onPress={() => this._doRemove()}/>

                    <View style={{height: 15}}/>
                    <TouchButton disabled={false} title={'读取文件'} onPress={() => this._doRead()}/>

                    <View style={{height: 15}}/>
                    <TouchButton disabled={false} title={'写入文件'} onPress={() => this._doWrite()}/>

                    <View style={{height: 15}}/>
                    <TouchButton disabled={false} title={'追加写入文件'} onPress={() => this._doAppend()}/>

                    <Text style={styles.content}>{this.state.message}</Text>
                </View>
            </ScrollView>
        );
    };

    _doGetPath() {
        this._setMessage('PATH:' + FileUtil.absoluteFile(FILE_PATH));
    }

    _doTestDoc() {
        this._setMessage('FILE_PATH:' + FILE_PATH);
        FileUtil.isExistDoc(FILE_PATH, (isExist) => {
            ToastAndroid.show('文件是否存在:' + isExist, ToastAndroid.SHORT);
        });
    }

    _doTestFolder() {
        this._setMessage('FILE_PATH:' + FILE_PATH);
        FileUtil.isExistFolder(FILE_PATH, (isExist) => {
            ToastAndroid.show('文件夹是否存在:' + isExist, ToastAndroid.SHORT);
        });
    }

    _doCreate() {
        this._setMessage('FILE_PATH:' + FILE_PATH);
        FileUtil.writeFile(FILE_PATH, "", (isSuccess) => {
            ToastAndroid.show('新建成功:' + isSuccess, ToastAndroid.SHORT);
        });
    }

    _doRemove() {
        this._setMessage('FILE_PATH:' + FILE_PATH);
        FileUtil.deleteFile(FILE_PATH, (isSuccess) => {
            ToastAndroid.show('删除成功:' + isSuccess, ToastAndroid.SHORT);
        });
    }

    _doRead() {
        this._setMessage('FILE_PATH:' + FILE_PATH);
        FileUtil.readFile(FILE_PATH,  (isSuccess) => {
            ToastAndroid.show('读取:' + isSuccess, ToastAndroid.SHORT);
        });
    }

    _doWrite() {
        this._setMessage('FILE_PATH:' + FILE_PATH);
        FileUtil.writeFile(FILE_PATH, this.textInfo, (isSuccess) => {
            ToastAndroid.show('写入:' + isSuccess, ToastAndroid.SHORT);
        });
    }

    /** --------------------------------------------------------------------------追加,自己后期修改的 */
    _doAppend() {
        this._setMessage('FILE_PATH:' + FILE_PATH);
        FileUtil.appendFile(FILE_PATH, this.textInfo, (isSuccess) => {
            ToastAndroid.show('追加写入:' + isSuccess, ToastAndroid.SHORT);
        });
    }

    _setMessage(message) {
        // 要更新界面就要setState
        this.setState({message: message});
    }
}

const styles = StyleSheet.create({
    container: {
        flexDirection: 'column',
        alignItems: 'center',
        // justifyContent: 'center',
    },
    content: {
        fontSize: 13,
        textAlign: 'left',
        margin: 10,
    },
});
